package com.javatiaocao.myblog.controller;

import com.javatiaocao.myblog.constant.CodeType;
import com.javatiaocao.myblog.model.Reward;
import com.javatiaocao.myblog.service.RewardService;
import com.javatiaocao.myblog.utils.DataMap;
import com.javatiaocao.myblog.utils.FileUtil;
import com.javatiaocao.myblog.utils.JsonResult;
import com.javatiaocao.myblog.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
public class RewardController {

    @Autowired
    private RewardService rewardService;

    /**
     * getRewardInfo
     * 获取募捐信息
     */
    @PostMapping("/getRewardInfo")
    public String getRewardInfo(){
        try {
            DataMap dataMap = rewardService.getRewardInfo();

            return JsonResult.build(dataMap).toJSON();

        } catch (Exception e) {
            log.error("getRewardInfo error" + e.getMessage());
        }

        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    /**
     *
     * 新增募捐记录
     *
     */
    @PostMapping("/addReward")
    public String addReward(@RequestParam("file") MultipartFile file, HttpServletRequest request, Reward reward){//接收io
        try {

            DataMap dataMap = rewardService.addReward(file,request,reward);//上传到数据库

            return JsonResult.build(dataMap).toJSON();

        } catch (Exception e) {
            log.error("addReward error" + e.getMessage());
        }

        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    /**
     *
     * 删除募捐记录
     *
     */
    @GetMapping("/deleteReward")
    public String deleteReward(Reward reward){
        try {

            DataMap dataMap = rewardService.deleteReward(reward);

            return JsonResult.build(dataMap).toJSON();

        } catch (Exception e) {
            log.error("deleteReward error" + e.getMessage());
        }

        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

}
